// pages/history/index/index.js
const api = require("../../utils/cloudAPIUtils");
const app = getApp();

Page({
  data: {
    tabCur: 0,          //选中的选项卡页
    tabItem:["工作中","已完成","已撤销"],

    pageNo:1,
    listData:[]
  },

  _loadListData(){
      if(this.data.pageNo > 0){
        let state = 1;
        if(this.data.tabCur == 1){
            state = 2;
        }else if(this.data.tabCur == 2){
            state = 4;
        }
        const _self = this;
        api.post(api.url.orderList,{
            state:state,
            pageNo:this.data.pageNo
        },(res)=>{
            if(res.data.code == 200){
                let listData = _self.data.listData || []
                listData = listData.concat(res.data.body.orderItemList);
                _self.setData({listData:listData})
            }else if(res.data.code == 202){
                _self.data.pageNo = -1;
            }else if(res.data.msg){
                wx.showModal({
                  content: res.data.msg,
                    showCancel:false
                })
            }else{
                wx.showToast({
                  title: '获取数据失败',
                  icon:'error'
                })
            }
        })
      }
  },

  //切换选项卡列表
  btnSelect(e) {
    const index = e.currentTarget.dataset.id;
    this.setData({
        listData:[],
        tabCur: index,
        pageNo:1
    })
    this._loadListData()
  },

  //跳转到设备详情页
  toInfo(e){
    let index = e.currentTarget.dataset.index;
    let item = this.data.listData[index];
    if(item.businessType == 'CDB'){
        wx.navigateTo({
            url:'/pages/order/cdb/detail?tradeNo='+item.tradeNo
        })
    }else if(item.businessType == 'CDCZ'){
        wx.navigateTo({
            url:'/pages/order/cdcz/detail?tradeNo='+item.tradeNo
        })
    }else{
        wx.showToast({
          title: '暂不支持查看',
          icon:'none'
        })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
      this.setData({
        pageNo:1,
        listData:[]
      })
      setTimeout(()=>{
          this._loadListData();
          wx.stopPullDownRefresh();
      },800)
  },

  //触底分页
  onScrolltolower(){
    this._loadListData();
  },

  onLoad(options) {
    const _self = this;
    //获取页面高度 - .nav盒子高度
    wx.getSystemInfo({
      success: (res) => {
        wx.createSelectorQuery().select('.nav').boundingClientRect(rests=>{
            _self.setData({scrollHeight:res.windowHeight - rests.bottom})
        }).exec();
      },
    })
    this._loadListData();
  },

  onShow(){

  }
})